home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Parsing an input stream?
- Date: 26 Feb 1996 09:24:51 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4gsqd3INNl2@anvil.ugrad.cs.ubc.ca>
- References: <4gsep9$ho8@srvr1.engin.umich.edu>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4gsep9$ho8@srvr1.engin.umich.edu>,
- byron lee newberry <newberry@news-server.engin.umich.edu> wrote:
- >Hello,
- >
- >I am currently working on coding a symbolic manipulation program to perform
- >tasks such as mutliplying out large polynomials, etc. I have a pretty
- >good idea how to program the beast except for the parsing of the input
- >stream. I have built up a very simple parser using basic string manipulation
- >functions, but it is very crued. Are libraries around that make this
- >a simplier job? It would seem to me that parsing would be a common task.
- >I appreciate any and all advice. Thanks for your time.
-
- I'd take the other poster's advice. Even if you are not working on UNIX, having
- your parser code prepared by lex and yacc is so much easier than hand-coding
- everything, and far more _maintainable_. It's easier to modify an abstract
- grammar specification with embedded C actions than a higgledy-piggledy mass of
- code.
-
- The tools are also efficient. For example, code generated by lex is must faster
- than naively written C. GNU Flex, in particular, has a number of options for
- space/time tradeoffs enabling you to generate small lexical analyzers that are
- slow, or faster ones that use larger tables.
-
- An example of a naively written lexical analyzer would be code which extracts a
- word from the input stream, and then compares it to a list of identifiers in
- sequence using strcmp(). Lex, on the other hand, can generate a single state
- machine that will accept any of those words simultaneously, and enter an
- acceptance state that can execute your C snippet.
-
- A robust lex specification will result in a lexical analyzer function that can
- easily deal with comments, whitespace, identifiers, constants: in short, any
- kinds of tokens that you can dream of that can be matched by a regular
- expression.
-
- Where lex stops, yacc takes off: you give it a grammar interspersed with C
- snippets, and it generates a parser that works in conjunction with lex (or
- possibly a hand-coded lexical analyzer function). You control the parser by
- carefully controlling the way you write the grammar. A typical use for yacc
- generated parsers is to construct an n-ary syntax tree which can be subject to
- further analysis elsewhere (type checking, translation, evaluation, etc).
-
- A yacc-generated parser will automatically detect syntax errors and call an
- error handling function. As you become a Master Yaccster, you will learn to
- implement error productions, which are ``dummy'' elements added to the grammar
- to catch common user mistakes and attempt to do recovery and continue parsing.
- You can have your parser do things like say ``warning: you missed a closing
- brace in line 3''.
-
- If you use lex and yacc, you can write a parser for a moderately complex
- programming language in one afternoon---even one that includes some rudimentary
- tree-building actions.
- --
-
-